package cn.ibizlab.odoo.core.odoo_digest.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Idigest_digest;
import cn.ibizlab.odoo.core.odoo_digest.domain.Digest_digest;
import cn.ibizlab.odoo.core.odoo_digest.filter.Digest_digestSearchContext;
import cn.ibizlab.odoo.core.odoo_digest.service.IDigest_digestService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[摘要] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Digest_digestServiceImpl implements IDigest_digestService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Idigest_digestClientService digest_digestClientService;

    @Override
    public boolean create(Digest_digest digest_digest) {
        boolean bOk = false ;
        Idigest_digest clientModel = this.convert2Model(digest_digest,null);
        digest_digestClientService.create(clientModel) ;
        this.convert2Domain(clientModel,digest_digest);
        bOk = true ;

        return bOk ;
    }

    @Override
    public void updateBatch(List<Digest_digest> digest_digests, int batchSize) {

        //digest_digestClientService.updateBatch(digest_digests,batchSize) ;

    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //digest_digestClientService.removeBatch(digest_digests) ;

    }

    @Override
    public Digest_digest get(Integer id) {
        Idigest_digest clientModel = digest_digestClientService.createModel();
        clientModel.setId(id);
        digest_digestClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Digest_digest get(Digest_digest digest_digest) {
        Digest_digest retdigest_digest = this.get(digest_digest.getId()) ;
    	BeanCopier copier=BeanCopier.create(retdigest_digest.getClass(), digest_digest.getClass(), false);
        copier.copy(retdigest_digest, digest_digest, null);
        return digest_digest ;
    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Idigest_digest clientModel = digest_digestClientService.createModel();
        clientModel.setId(id);
        digest_digestClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean update(Digest_digest digest_digest) {
        boolean bOk = false ;
        Idigest_digest clientModel = this.convert2Model(digest_digest,null);
        digest_digestClientService.update(clientModel) ;
        this.convert2Domain(clientModel,digest_digest) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void createBatch(List<Digest_digest> digest_digests, int batchSize) {

                        //digest_digestClientService.createBatch(digest_digests,batchSize) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Digest_digest> searchDefault(Digest_digestSearchContext context) {
        List<Digest_digest> list = new ArrayList<Digest_digest>();
        Page<cn.ibizlab.odoo.core.client.model.Idigest_digest> clientModelList = digest_digestClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Idigest_digest clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Digest_digest>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据EMail模板查询
     */
    @Override
    public Page<Digest_digest> selectByOdoo_template(Integer id){
        Digest_digestSearchContext context = new Digest_digestSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Digest_digest> list = new ArrayList<Digest_digest>();
        Page<cn.ibizlab.odoo.core.client.model.Idigest_digest> clientModelList = digest_digestClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Idigest_digest clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Digest_digest>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据公司查询
     */
    @Override
    public Page<Digest_digest> selectByOdoo_company(Integer id){
        Digest_digestSearchContext context = new Digest_digestSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Digest_digest> list = new ArrayList<Digest_digest>();
        Page<cn.ibizlab.odoo.core.client.model.Idigest_digest> clientModelList = digest_digestClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Idigest_digest clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Digest_digest>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Digest_digest> selectByOdoo_create(Integer id){
        Digest_digestSearchContext context = new Digest_digestSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Digest_digest> list = new ArrayList<Digest_digest>();
        Page<cn.ibizlab.odoo.core.client.model.Idigest_digest> clientModelList = digest_digestClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Idigest_digest clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Digest_digest>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Digest_digest> selectByOdoo_write(Integer id){
        Digest_digestSearchContext context = new Digest_digestSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Digest_digest> list = new ArrayList<Digest_digest>();
        Page<cn.ibizlab.odoo.core.client.model.Idigest_digest> clientModelList = digest_digestClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Idigest_digest clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Digest_digest>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Idigest_digest convert2Model(Digest_digest domain , Idigest_digest model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = digest_digestClientService.createModel() ;
        }

         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getKpi_website_sale_total_valueDirtyFlag())
            model.setKpi_website_sale_total_value(domain.getKpi_website_sale_total_value());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getKpi_crm_lead_created_valueDirtyFlag())
            model.setKpi_crm_lead_created_value(domain.getKpi_crm_lead_created_value());
         if(domain.getKpi_crm_opportunities_wonDirtyFlag())
            model.setKpi_crm_opportunities_won(domain.getKpi_crm_opportunities_won());
         if(domain.getAvailable_fieldsDirtyFlag())
            model.setAvailable_fields(domain.getAvailable_fields());
         if(domain.getStateDirtyFlag())
            model.setState(domain.getState());
         if(domain.getUser_idsDirtyFlag())
            model.setUser_ids(domain.getUser_ids());
         if(domain.getKpi_crm_opportunities_won_valueDirtyFlag())
            model.setKpi_crm_opportunities_won_value(domain.getKpi_crm_opportunities_won_value());
         if(domain.getKpi_hr_recruitment_new_colleaguesDirtyFlag())
            model.setKpi_hr_recruitment_new_colleagues(domain.getKpi_hr_recruitment_new_colleagues());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getKpi_pos_totalDirtyFlag())
            model.setKpi_pos_total(domain.getKpi_pos_total());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getKpi_pos_total_valueDirtyFlag())
            model.setKpi_pos_total_value(domain.getKpi_pos_total_value());
         if(domain.getKpi_website_sale_totalDirtyFlag())
            model.setKpi_website_sale_total(domain.getKpi_website_sale_total());
         if(domain.getKpi_all_sale_totalDirtyFlag())
            model.setKpi_all_sale_total(domain.getKpi_all_sale_total());
         if(domain.getNext_run_dateDirtyFlag())
            model.setNext_run_date(domain.getNext_run_date());
         if(domain.getKpi_res_users_connectedDirtyFlag())
            model.setKpi_res_users_connected(domain.getKpi_res_users_connected());
         if(domain.getKpi_res_users_connected_valueDirtyFlag())
            model.setKpi_res_users_connected_value(domain.getKpi_res_users_connected_value());
         if(domain.getPeriodicityDirtyFlag())
            model.setPeriodicity(domain.getPeriodicity());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getKpi_hr_recruitment_new_colleagues_valueDirtyFlag())
            model.setKpi_hr_recruitment_new_colleagues_value(domain.getKpi_hr_recruitment_new_colleagues_value());
         if(domain.getKpi_account_total_revenueDirtyFlag())
            model.setKpi_account_total_revenue(domain.getKpi_account_total_revenue());
         if(domain.getKpi_crm_lead_createdDirtyFlag())
            model.setKpi_crm_lead_created(domain.getKpi_crm_lead_created());
         if(domain.getKpi_project_task_openedDirtyFlag())
            model.setKpi_project_task_opened(domain.getKpi_project_task_opened());
         if(domain.getKpi_all_sale_total_valueDirtyFlag())
            model.setKpi_all_sale_total_value(domain.getKpi_all_sale_total_value());
         if(domain.getIs_subscribedDirtyFlag())
            model.setIs_subscribed(domain.getIs_subscribed());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getKpi_project_task_opened_valueDirtyFlag())
            model.setKpi_project_task_opened_value(domain.getKpi_project_task_opened_value());
         if(domain.getKpi_account_total_revenue_valueDirtyFlag())
            model.setKpi_account_total_revenue_value(domain.getKpi_account_total_revenue_value());
         if(domain.getKpi_mail_message_total_valueDirtyFlag())
            model.setKpi_mail_message_total_value(domain.getKpi_mail_message_total_value());
         if(domain.getKpi_mail_message_totalDirtyFlag())
            model.setKpi_mail_message_total(domain.getKpi_mail_message_total());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getTemplate_id_textDirtyFlag())
            model.setTemplate_id_text(domain.getTemplate_id_text());
         if(domain.getCompany_id_textDirtyFlag())
            model.setCompany_id_text(domain.getCompany_id_text());
         if(domain.getCurrency_idDirtyFlag())
            model.setCurrency_id(domain.getCurrency_id());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getCompany_idDirtyFlag())
            model.setCompany_id(domain.getCompany_id());
         if(domain.getTemplate_idDirtyFlag())
            model.setTemplate_id(domain.getTemplate_id());
        return model ;
    }

    public Digest_digest convert2Domain( Idigest_digest model ,Digest_digest domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Digest_digest();
        }

        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getKpi_website_sale_total_valueDirtyFlag())
            domain.setKpi_website_sale_total_value(model.getKpi_website_sale_total_value());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getKpi_crm_lead_created_valueDirtyFlag())
            domain.setKpi_crm_lead_created_value(model.getKpi_crm_lead_created_value());
        if(model.getKpi_crm_opportunities_wonDirtyFlag())
            domain.setKpi_crm_opportunities_won(model.getKpi_crm_opportunities_won());
        if(model.getAvailable_fieldsDirtyFlag())
            domain.setAvailable_fields(model.getAvailable_fields());
        if(model.getStateDirtyFlag())
            domain.setState(model.getState());
        if(model.getUser_idsDirtyFlag())
            domain.setUser_ids(model.getUser_ids());
        if(model.getKpi_crm_opportunities_won_valueDirtyFlag())
            domain.setKpi_crm_opportunities_won_value(model.getKpi_crm_opportunities_won_value());
        if(model.getKpi_hr_recruitment_new_colleaguesDirtyFlag())
            domain.setKpi_hr_recruitment_new_colleagues(model.getKpi_hr_recruitment_new_colleagues());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getKpi_pos_totalDirtyFlag())
            domain.setKpi_pos_total(model.getKpi_pos_total());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getKpi_pos_total_valueDirtyFlag())
            domain.setKpi_pos_total_value(model.getKpi_pos_total_value());
        if(model.getKpi_website_sale_totalDirtyFlag())
            domain.setKpi_website_sale_total(model.getKpi_website_sale_total());
        if(model.getKpi_all_sale_totalDirtyFlag())
            domain.setKpi_all_sale_total(model.getKpi_all_sale_total());
        if(model.getNext_run_dateDirtyFlag())
            domain.setNext_run_date(model.getNext_run_date());
        if(model.getKpi_res_users_connectedDirtyFlag())
            domain.setKpi_res_users_connected(model.getKpi_res_users_connected());
        if(model.getKpi_res_users_connected_valueDirtyFlag())
            domain.setKpi_res_users_connected_value(model.getKpi_res_users_connected_value());
        if(model.getPeriodicityDirtyFlag())
            domain.setPeriodicity(model.getPeriodicity());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getKpi_hr_recruitment_new_colleagues_valueDirtyFlag())
            domain.setKpi_hr_recruitment_new_colleagues_value(model.getKpi_hr_recruitment_new_colleagues_value());
        if(model.getKpi_account_total_revenueDirtyFlag())
            domain.setKpi_account_total_revenue(model.getKpi_account_total_revenue());
        if(model.getKpi_crm_lead_createdDirtyFlag())
            domain.setKpi_crm_lead_created(model.getKpi_crm_lead_created());
        if(model.getKpi_project_task_openedDirtyFlag())
            domain.setKpi_project_task_opened(model.getKpi_project_task_opened());
        if(model.getKpi_all_sale_total_valueDirtyFlag())
            domain.setKpi_all_sale_total_value(model.getKpi_all_sale_total_value());
        if(model.getIs_subscribedDirtyFlag())
            domain.setIs_subscribed(model.getIs_subscribed());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getKpi_project_task_opened_valueDirtyFlag())
            domain.setKpi_project_task_opened_value(model.getKpi_project_task_opened_value());
        if(model.getKpi_account_total_revenue_valueDirtyFlag())
            domain.setKpi_account_total_revenue_value(model.getKpi_account_total_revenue_value());
        if(model.getKpi_mail_message_total_valueDirtyFlag())
            domain.setKpi_mail_message_total_value(model.getKpi_mail_message_total_value());
        if(model.getKpi_mail_message_totalDirtyFlag())
            domain.setKpi_mail_message_total(model.getKpi_mail_message_total());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getTemplate_id_textDirtyFlag())
            domain.setTemplate_id_text(model.getTemplate_id_text());
        if(model.getCompany_id_textDirtyFlag())
            domain.setCompany_id_text(model.getCompany_id_text());
        if(model.getCurrency_idDirtyFlag())
            domain.setCurrency_id(model.getCurrency_id());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getCompany_idDirtyFlag())
            domain.setCompany_id(model.getCompany_id());
        if(model.getTemplate_idDirtyFlag())
            domain.setTemplate_id(model.getTemplate_id());
        return domain ;
    }


}
